<?php

namespace  app\service;


use app\model\PartnerLogModel;
use app\model\PartnerModel;
use app\model\PartnerRoleModel;
use app\model\PartnerUserModel;
use app\model\PermissionModel;
use app\model\RoleModel;
use Couchbase\Role;
use think\facade\Db;

Class PermissionService{
    // todo 根据partner_id选择不同的权限列表
    public static  function getAllPermission()
    {
        return PermissionModel::GetAllPermission();
    }

    //
    public static  function getAllPermissionWhere($where)
    {
        return PermissionModel::GetAllPermissionWhere($where);
    }

    public static  function getAllRole()
    {
        return PermissionModel::GetAllRole();
    }

    //根据uid获取所有的权限

    public static function getAllPermissionByUid($uid =0) {
        $roleRes=PartnerUserModel::getRoleByUid($uid);
        if (!$roleRes){
            return [];
        }
        $role_id=$roleRes['role_id'];
        if ($role_id>0){
            $permissions=RoleModel::getRoleByUid($role_id);
            $permission_ids=$permissions['permission_ids'];
            $where=[
                ['id','in',$permission_ids],
                ['type','=',1],
            ];

            $data=PermissionService::getAllPermissionWhere($where);
            if ($data){
                return $data;
            }else{
                return [];
            }

        }else{
            return [];
        }
    }

    //判断当前uid是否有权限

    public static function getPermissionByUid($uid =0,$url) :bool{
        $roleRes=PartnerUserModel::getRoleByUid($uid);
        if (!$roleRes){
            return false;
        }

        $role_id=$roleRes['role_id'];
        if ($role_id>0){
            $permissions=RoleModel::getRoleByUid($role_id);
            $permission_ids=$permissions['permission_ids'];
            if ($permission_ids=="*"){
                return true;
            }
            $where=[
                ['id','in',$permission_ids],
                ['menu_url','=',$url],
//                ['is_unauth','=',2],
                ['is_unlogin','=',1],
            ];

            $data=PermissionService::getAllPermissionWhere($where);
            if ($data){
                return true;
            }else{
                return false;
            }

        }else{
            return false;
        }
    }


    public static  function add_permission($param) :int
    {
        //查询mobile是否存在
        $where=[
            'cname'=>$param['cname'],
            'fname'=>$param['fname']
        ];

        $PermissionInfo=PermissionModel::getPermissionInfo($where);
        if (!empty($PermissionInfo)){
            return  10001;
        }


        $data=[
            'name'=>$param['name'],
            'add_time'=>getNowDateTime(),
            'status'=>$param['status'],
            'mname'=>$param['name'],
            'cname'=>$param['cname'],
            'fname'=>$param['fname'],
            'pid'=>$param['pid'],
            'type'=>$param['type'],
            'menu_url'=>'/'.$param['cname']."/".$param['fname'],
            'is_unauth'=>$param['is_unauth'],
            'is_unlogin'=>$param['is_unlogin'],
            'partner_id'=>$param['partner_id'],
            'system_ids'=>$param['system_ids'],
            'btn_name' => $param['btn_name'],
        ];
        Db::startTrans();
        $addPermissionId=PermissionModel::add_permission($data);
        $dataLog=[
            'partner_user_id'=>$param['uid'],
            'log_type'=>2,
            'request_ip'=>$param['ip'],
            'add_time'=>getNowDateTime(),
            'log_desc'=>'新增合作商权限',
            'status'=>1,
            'partner_id'=>$param['partner_id']
        ];
        
        if ($addPermissionId){
            Db::commit();
            return 10000;
        }else{
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }

    public static  function modify_permission($param,$permission_id) :int
    {
        //查询mobile是否存在
        $where=[
            'id'=>$permission_id,
        ];

        $PermissionInfo=PermissionModel::getPermissionInfo($where);
        if (empty($PermissionInfo)){
            return  10001;
        }


        $data=[
            'name'=>$param['name'],
            'update_time'=>getNowDateTime(),
            'status'=>$param['status'],
            'mname'=>$param['name'],
            'cname'=>$param['cname'],
            'fname'=>$param['fname'],
            'pid'=>$param['pid'],
            'type'=>$param['type'],
            'menu_url'=>'/'.$param['cname']."/".$param['fname'],
            'is_unauth'=>$param['is_unauth'],
            'is_unlogin'=>$param['is_unlogin'],
            'partner_id'=>$param['partner_id'],
            'system_ids'=>$param['system_ids'],
            'btn_name' => $param['btn_name'],

        ];
        Db::startTrans();
        $addPermissionId=PermissionModel::modify_permission($data,$permission_id);
        $dataLog=[
            'partner_user_id'=>$param['uid'],
            'log_type'=>2,
            'request_ip'=>$param['ip'],
            'add_time'=>getNowDateTime(),
            'log_desc'=>'修改合作商权限',
            'status'=>1,
            'partner_id'=>$param['partner_id']
        ];
        
        if ($addPermissionId){
            Db::commit();
            return 10000;
        }else{
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }

    public static  function del_permission($param,$permission_id) :int
    {
        //查询mobile是否存在
        $where=[
            'id'=>$permission_id,
            'status'=>1
        ];

        $PermissionInfo=PermissionModel::getPermissionInfo($where);
        if (empty($PermissionInfo)){
            return  10001;
        }


        Db::startTrans();
        $data=['status'=>3];
        $addPermissionId=PermissionModel::modify_permission($data,$permission_id);
        $dataLog=[
            'partner_user_id'=>$param['uid'],
            'log_type'=>2,
            'request_ip'=>$param['ip'],
            'add_time'=>getNowDateTime(),
            'log_desc'=>'删除合作商权限',
            'status'=>1,
            'partner_id'=>$param['partner_id']
        ];
        
        if ($addPermissionId){
            Db::commit();
            return 10000;
        }else{
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }

    public  static   function  log($param){
        return '';
    }

}